package com.stenio.fancat.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class IOUtil {

	public static void main(String[] args) throws IOException {
		String headimgurl = "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83er3FAKeM4ZRgzaCAdcrOdUAnjwicoRribopLFc61dZC3Ejibm4K1uG6BfF7Cqggm0mm2H5KvMmelAHfA/132";
		toByteArray(headimgurl);
	}

	/**
	 * 通过图片的URL转换为byte[]类型，用于保存到blob字段中
	 * @param imgURL 图片的URL
	 * @return
	 * @throws IOException
	 */
	public static byte[] toByteArray(String imgURL) throws IOException {
		return toByteArray(new URL(imgURL).openStream());
	}

	public static byte[] toByteArray(InputStream input) throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] buffer = new byte[4096];
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
		}
		return output.toByteArray();
	}

}
